<template>
  <div>
    <!-- 搜索框 -->
    <div class="search">
      <p>策略搜索：</p>
      <div class="searchbox">
        <el-input v-model="input" placeholder="请输入"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="searchStrategy"
          >搜索</el-button
        >
      </div>
    </div>
    <el-button
      type="primary"
      icon="el-icon-circle-plus-outline"
      class="add"
      @click="addNew"
      >新建</el-button
    >
    <!-- 表格 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="序号" type="index" width="180"> </el-table-column>
      <el-table-column prop="policyName" label="策略名称" width="180">
      </el-table-column>
      <el-table-column prop="discount" label="策略方案"> </el-table-column>
      <el-table-column prop="createTime" label="创建日期"> </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            type="text"
            class="left"
            @click="strategyInfo(scope.row.policyId, scope.row)"
            >查看详情</el-button
          >
          <el-button type="text" class="left" @click="edit(scope.row)"
            >修改</el-button
          ><el-button type="text" class="right" @click="del(scope.row.policyId)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 底部上下页 -->
    <div class="footer">
      <div class="left">
        共<span>{{ msg.totalCount }}</span
        >条记录 第{{ msg.pageIndex }}/{{ msg.totalPage }}页
      </div>
      <div class="button">
        <el-button
          class="prev"
          @click="changeNum(-1)"
          :disabled="pageIndex === 1"
          >上一页</el-button
        ><el-button
          type="primary"
          class="next"
          @click="changeNum(1)"
          :disabled="pageIndex >= msg.totalPage"
          >下一页</el-button
        >
      </div>
    </div>
    <!-- 新增弹出框 -->
    <el-dialog title="新增策略" :visible.sync="dialogVisible" width="50%">
      <el-form label-width="150px" :model="form" :rules="rules">
        <el-form-item label="策略名称" prop="policyName">
          <el-input type="text" placeholder="请输入" v-model="form.policyName">
          </el-input>
        </el-form-item>
        <el-form-item label="策略方案" prop="discount">
          <el-input-number
            v-model="form.discount"
            controls-position="right"
            :min="1"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addStrategy">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改弹出框 -->
    <el-dialog title="修改策略" :visible.sync="dialogVisible2" width="50%">
      <el-form label-width="150px" :model="form" :rules="rules">
        <el-form-item label="策略名称" prop="policyName">
          <el-input
            type="text"
            placeholder="请输入"
            v-model="form.policyName"
            maxlength="15"
            show-word-limit
          >
          </el-input>
        </el-form-item>
        <el-form-item label="策略方案" prop="discount">
          <el-input-number
            v-model="form.discount"
            controls-position="right"
            :min="1"
            placeholder="请输入"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="editStrategy">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 策略详情弹出框 -->
    <el-dialog title="策略详情" :visible.sync="dialogVisible1" width="50%">
      <div>策略名称：{{ this.policyName1 }}</div>
      <div>策略方案</div>
      <!-- 策略方案中的表格 -->
      <el-table :data="tableData1" style="width: 100%">
        <el-table-column type="index" label="序号" width="180">
        </el-table-column>
        <el-table-column prop="nodeName" label="点位名称" width="180">
        </el-table-column>
        <el-table-column prop="innerCode" label="设备编号"> </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer"> </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  searchStrategy,
  getStrategy,
  strategyInfo,
  delStrategy,
  editStrategy
} from '@/api/strategy'
export default {
  name: 'strategyPage',
  components: {},
  props: {},
  data() {
    return {
      input: '',
      tableData: [],
      pageIndex: 1,
      pageSize: 10,
      dialogVisible: false,
      dialogVisible1: false,
      dialogVisible2: false,
      num: '',
      msg: {},
      form: {
        policyName: '',
        discount: undefined
      },
      tableData1: [],
      rules: {
        discount: [{ required: true, trigger: 'blur' }],
        policyName: [{ required: true, trigger: 'blur' }]
      },
      id: null,
      policyName1: ''
    }
  },
  computed: {},
  watch: {},
  async created() {
    this.initData()
  },
  methods: {
    // 页面渲染
    async initData() {
      const obj = {
        pageIndex: this.pageIndex,
        pageSize: this.pageSize
      }
      const res = await searchStrategy(obj)
      console.log(res)
      this.tableData = res.currentPageRecords
      this.msg = res
      this.tableData.forEach((item) => {
        item.createTime = item.createTime.replace('T', ' ')
      })
    },
    // 数据搜索
    async searchStrategy() {
      const obj = {
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
        policyName: this.input
      }
      const res = await searchStrategy(obj)
      // console.log(res)
      this.tableData = res.currentPageRecords
      this.input = ''
    },
    // 打开弹出框
    addNew() {
      this.dialogVisible = true
    },
    // 新增策略
    async addStrategy() {
      await getStrategy(this.form)
      this.initData(this.form)
      this.dialogVisible = false
      this.form.policyName = ''
    },
    // 策略详情
    async strategyInfo(id, row) {
      console.log(id, row)
      const obj = {
        pageIndex: this.pageIndex,
        pageSize: this.pageSize
      }
      const res = await strategyInfo(obj, id)
      this.policyName1 = row.policyName
      console.log(res)
      this.tableData1 = res.currentPageRecords
      this.dialogVisible1 = true
    },
    // 上下页
    changeNum(num) {
      this.pageIndex += num
      this.initData()
    },
    // 删除
    async del(id) {
      console.log(id)
      const obj = {
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
        policyName: this.form.policyName
      }
      await delStrategy(obj, id)
      this.initData()
    },
    // 弹出框回显
    edit(row) {
      console.log(row)
      this.dialogVisible2 = true
      this.form = { ...row }
      this.id = row.policyId
    },
    async editStrategy() {
      const obj = {
        policyName: this.form.policyName,
        discount: this.form.discount
      }
      await editStrategy(obj, this.id)
      this.dialogVisible2 = false
      this.initData()
    }
  }
}
</script>

<style scoped lang="scss">
.search {
  display: flex;
  align-items: center;
  .el-input {
    width: 200px;
    margin: 20px;
    color: #6188fc;
  }
  .el-button {
    color: #fff;
    background-color: #6188fc;
    border: 0;
  }
}
.add {
  background-color: #fc6733;
  color: #fff;
  border: 0;
  margin-top: 50px;
  margin-bottom: 20px;
  &:hover {
    background-color: #fc6733;
  }
}
.footer {
  display: flex;
  align-items: center;
  .left {
    color: #dbdfe5;
    font-weight: 400;
    margin-top: 40px;
    margin-right: 800px;
  }
  .button {
    margin-top: 40px;
    .prev {
      background-color: #edf0f9;
    }
    .next {
      background-color: #d6def7;
      color: #606266;
      border: 0;
    }
  }
}
</style>
